<template>
	<div class="yellow">
		<div class="head flex a-center jlr">
			<div class="back" @click="back"><i class="iconfont icon-arrow-left-copy"></i></div>
			<h2>登录</h2>
			<div class="back">&nbsp;</div>
		</div>

		<div class="abox">
			<div class="login">
				<div class="portrait"></div>
				<div class="loginbox">
					<div class="loginItem flex a-center">
						<input type="tel" placeholder="请输入手机号" maxlength="11" v-model="obj.phone">
						<div class="icon-close" @click="openclose" v-if="obj.phone"></div>
					</div>
					<div class="loginItem flex a-center">
						<input :type="type?'password':'text'" placeholder="请输入密码" v-model="obj.passwd">
						<i class="iconfont" :class="type?'icon-yanjing':'icon-yanjing1'" @click="openpwd"></i>
					</div>

					<div class="forget" @click="forget">忘记密码？</div>

					<div class="blockbtn" @click="login">登录</div>
					<div class="regbtn" @click="enter('/reg')">注册</div>
				</div>
			</div>

		</div>

	</div>

</template>
<script>
export default {
	data(){
		return {
			type:true,
			obj:{
				phone:'',
				passwd:''
			}
		}
	},
	methods:{
		enter(v){
			this.$router.push(v)
		},
		back(){
			// this.$router.go(-1)
      this.$router.replace(this.$route.query.from || '/');

		},
    forget() {
		  this.$router.push("/forget");
    },
		openclose(){
			this.obj.phone = ''
		},
		openpwd(){
			this.type = !this.type
		},
		login(){
			var that = this
			var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
			if(!myreg.test(that.obj.phone)){
				that.$toast.fail('手机号格式不正确')
			}else if(!that.obj.passwd){
				that.$toast.fail('请输入密码')
			}else{
				that.$ajax.login({...that.obj}).then(res=>{
					if(res.code==0){
						localStorage.setItem('token',res.data.token)
						localStorage.setItem('memberId',res.data.memberId)
						this.$router.push(this.$route.query.redirect || '/');
					}
				})
			}

		}
	}
}
</script>
